package com.soda.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.soda.bean.Student;
import com.soda.service.StudentService;
import com.soda.service.impl.StudentServiceImpl;

public class QianLoginServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		request.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		StudentService stus = new StudentServiceImpl();
		Student s = new Student();
		String method=request.getParameter("method");
		
		if(method!=null){
			if (method.equals("login")) {
				//String inp = request.getParameter("yan");
				/*String srand = session.getAttribute("code").toString();
				if (!inp.equalsIgnoreCase(srand)) {
					out.print("<script>alert('验证码错误');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;
				}*/
				String name = request.getParameter("uname");
				String pwd = request.getParameter("pwd");
				s = stus.selectstuNameByLoginNamePwd(name, pwd);
				if (s != null) {
					session.setAttribute("student", s);
					out.print("<script>alert('登录成功');</script>");
					out.print("<script>location.href='ShowQianTaiServletYu';</script>");
					return;
				} else {
					out.print("<script>alert('用户名不存在，或密码错误');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;

				}
			}
			/**
			 * 退出操作
			 */
			if (method.equals("tuichu")) {
				if(session.getAttribute("student")==null){
					out.print("<script>alert('您还未登录');</script>");
					out.print("<script>location.href='ShowQianTaiServletYu';</script>");
					return;
				}
				session.invalidate();
				out.print("<script>alert('退出成功');</script>");
				out.print("<script>location.href='ShowQianTaiServletYu';</script>");
				return;
			}
			/**
			 * 宗矿帅 注册传值
			 */
			if (method.equals("regist")) {
				s.setStuLoginname(request.getParameter("uname"));
				s.setStuPassword(request.getParameter("pwd"));
				s.setStuName(request.getParameter("rname"));
				s.setStuIdcard(request.getParameter("uid"));
				s.setStuSex(request.getParameter("gender").equals("男") ? 1 : 0);
				s.setStuPhone(request.getParameter("phone"));
				s.setStuAddress(request.getParameter("address"));
				s.setStuEmail(request.getParameter("email"));
				s.setStuSchool(request.getParameter("school").trim() == "" ? ""
						: request.getParameter("school"));
				s.setStuMajor(request.getParameter("major").trim() == "" ? ""
						: request.getParameter("major"));
				s.setStuRemark(request.getParameter("remark").trim() == "" ? ""
						: request.getParameter("remark"));
				int y = stus.addRegistStuMessage(s);
				if (y == 1) {
					out.print("<script>alert('注册成功，请登录');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;

				} else {
					out.print("<script>alert('注册失败');</script>");
					out.print("<script>location.href='qiantai/regist.jsp';</script>");
					return;
				}
			}
			
			/**
			 * 宗矿帅获取注册页面ajax的传值判断要注册的用户名是否已经存在
			 */
			if (method.equals("ajregist")) {
			
				String name = new String(request.getParameter("name").getBytes(
						"ISO-8859-1"), "utf-8");
				System.out.println(name);
				int x = stus.selectajaxName(name);
				if (x == 1) {
					out.print(1);
				} else {
					out.print(0);
				}
				out.close();
				return;
			}

			/**
			 * 修改个人信息
			 */
			if (method.equals("xiugaixinxi")) {
				Student student = (Student) session.getAttribute("student");
				if (student == null) {
					out.print("<script>alert('请先登录');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;
				}
				student.setStuPhone(request.getParameter("phone").trim() == "" ? student
						.getStuPhone() : request.getParameter("phone"));
				student.setStuAddress(request.getParameter("address").trim() == "" ? student
						.getStuAddress() : request.getParameter("address"));
				student.setStuEmail(request.getParameter("email").trim() == "" ? student
						.getStuEmail() : request.getParameter("email"));
				student.setStuSchool(request.getParameter("school") == "" ? student
						.getStuSchool() : request.getParameter("school"));
				student.setStuMajor(request.getParameter("major") == "" ? student
						.getStuMajor() : request.getParameter("major"));
				student.setStuRemark(request.getParameter("remark") == "" ? student
						.getStuRemark() : request.getParameter("remark"));
				session.setAttribute("student", student);
				int stuid = student.getStuId();
				int x = stus.updateMessage(stuid, student);
				if (x == 1) {
					out.print("<script>alert('修改成功');</script>");
					out.print("<script>location.href='qiantai/message.jsp';</script>");
					return;
				} else {
					out.print("<script>alert('修改失败');</script>");
					out.print("<script>location.href='qiantai/msgUpdate.jsp';</script>");
					return;
				} 
			}
			
			/**
			 * ajax判断原密码,以及修改密码
			 */
			if (method.equals("ajaxpwd")) {
				Student student = (Student) request.getSession().getAttribute(
						"student");
				if(student==null){
					out.print("<script>alert('请先登录');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;
				}
				String oldpwd = new String(request.getParameter("pwd")
						.getBytes("ISO-8859-1"), "utf-8");
				int stuid = student.getStuId();
				if (student != null) {
					s = stus.selectStumessage(stuid);
					if (!oldpwd.equals(s.getStuPassword())) {
						out.print(0);
					} else {
						out.print(1);
					}
					out.close();
					return;
				}
			}
			if (method.equals("xiugaipwd")) {
				Student student = (Student) request.getSession().getAttribute(
						"student");
				if(student==null){
					out.print("<script>alert('请先登录');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;
				}
				String newpwd = request.getParameter("newpwd");
				int stuid = student.getStuId();
				if (student != null) {
					int n = stus.updatePwd(stuid, newpwd);
					if (n == 1) {
						session.invalidate();
						out.print("<script>alert('修改成功，请登录');</script>");
						out.print("<script>location.href='qiantai/login.jsp';</script>");
						return;
					} else {
						out.print("<script>alert('修改失败!');</script>");
						out.print("<script>location.href='qiantai/password.jsp';</script>");
						return;
					}
				}
			}

			/**
			 * 充值
			 */
			if (method.equals("ajaxchong")) {
				Student student = (Student) request.getSession().getAttribute(
						"student");
				if (student == null) {
					out.print("<script>alert('请先登录');</script>");
					out.print("<script>location.href='qiantai/login.jsp';</script>");
					return;
				}
				String mon = request.getParameter("money");
				int money = Integer.parseInt(mon);
				int stuid = student.getStuId();
				int n = stus.chongzhiMoney(student, money);
				if (n == 1) {
					s = stus.selectMoneyChongZhiHou(stuid);
					session.setAttribute("student", s);
					out.print("<script>alert('充值成功');</script>");
					out.print("<script>location.href='qiantai/chongzhi.jsp';</script>");
					return;
				}
			}
			
		}
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
